package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetMathGameRecord;
import com.rjwh.dingdong.client.bean.localbean.MathGameListData;
import com.rjwh.dingdong.client.bean.localbean.MathGameRecordData;
import com.rjwh.dingdong.client.bean.localbean.MathGameRecordDataTotal;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MathGameRecordInfoModel;
import com.rjwh.dingdong.client.mvp.presenter.IMathGameRecordInfoPresenter;
import com.rjwh.dingdong.client.mvp.view.MathGameRecordInfoView;

import java.util.List;

/**
 * @author Kay
 * @ClassName MathGameRecordInfoPresenter
 * @Description
 * @date 2016-7-27
 * @history 2016-7-27 author: description:
 */
public class MathGameRecordInfoPresenter extends BasePresenter<MathGameRecordInfoView> implements IMathGameRecordInfoPresenter {

    private MathGameRecordInfoModel model;

    public MathGameRecordInfoPresenter(Context context, MathGameRecordInfoView mMvpView) {
        super(context, mMvpView);
        model = new MathGameRecordInfoModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETMATHGAMERECORD:
                    ResGetMathGameRecord resGetMathGameRecord = (ResGetMathGameRecord) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetMathGameRecord != null) {
                        MathGameRecordData data = resGetMathGameRecord.getData();
                        if (data != null) {
                            MathGameRecordDataTotal total = data.getTotal();
                            List<MathGameListData> gamelist = data.getGamelist();
                            getMvpView().setGameRecordData(total, gamelist);
                        }
                    }
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 获取游戏记录
     *
     * @param bookid 游戏id
     */
    @Override
    public void doGetMathGameRecord(String bookid) {
        if (isViewAttached()) {
            getMvpView().hideErrorView();
            getMvpView().showLoadingView();
            model.doGetMathGameRecord(bookid);
        }
    }
}
